/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.console;

import java.awt.BorderLayout;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

import org.diskordia.heroesforge.client.presentation.ICommand;
import org.diskordia.heroesforge.console.utils.GuiUtils;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import java.awt.Rectangle;
import java.awt.Dimension;

/**
 * @author lehmamic
 *
 */
@Singleton
public class MainFrame extends JFrame implements IMainView {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenu = null;
	private JMenu jMenuTeam = null;
	private JMenu jMenuHelp = null;
	private JMenuItem jMenuItemConnect = null;
	
	private ICommand commandConnect;  //  @jve:decl-index=0:
	
	private ResourceMap resourceMap = Application.getInstance(HeroesForge.class).getContext().getResourceMap(MainFrame.class);  //  @jve:decl-index=0:
	private ActionMap actionMap = Application.getInstance(HeroesForge.class).getContext().getActionMap(MainFrame.class, this);
	private JMenuItem jMenuItemExit = null;
	
	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		initialize();
		
		this.getJMenu().setText(this.resourceMap.getString("jMenuFile.text"));
		this.getJMenuTeam().setText(this.resourceMap.getString("jMenuTeam.text"));
		this.jMenuHelp.setText(this.resourceMap.getString("jMenuHelp.text"));
		
		GuiUtils.centerWithinScreen(this);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("Heroes Forge");
		this.setMinimumSize(new Dimension(1024, 680));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(new Rectangle(0, 0, 1024, 680));
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getJMenuTeam());
			jJMenuBar.add(getJMenuHelp());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("File");
			jMenu.add(getJMenuItemExit());
		}
		return jMenu;
	}

	/**
	 * This method initializes jMenuTeam	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuTeam() {
		if (jMenuTeam == null) {
			jMenuTeam = new JMenu();
			jMenuTeam.setText("Team");
			jMenuTeam.add(getJMenuItemConnect());
		}
		return jMenuTeam;
	}

	/**
	 * This method initializes jMenuHelp	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuHelp() {
		if (jMenuHelp == null) {
			jMenuHelp = new JMenu();
			jMenuHelp.setText("Help");
		}
		return jMenuHelp;
	}

	/**
	 * This method initializes jMenuItemConnect	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemConnect() {
		if (jMenuItemConnect == null) {
			jMenuItemConnect = new JMenuItem();
			jMenuItemConnect.setText("Connect to Heroes Forge server...");
			jMenuItemConnect.setAction(this.getConnectAction());
		}
		return jMenuItemConnect;
	}

	/**
	 * @return the connect action.
	 */
	private Action getConnectAction() {
		return this.actionMap.get("showConectionDialog");
	}
	
	/**
	 * @return the exit action
	 */
	private Action getExitAction() {
		return this.actionMap.get("quit");
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IView#CloseView()
	 */
	@Override
	public void CloseView() {
		this.setVisible(false);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IView#ShowView()
	 */
	@Override
	public void ShowView() {
		HeroesForge.getApplication().show(this);
	}

	/**
	 * @param connectCommand the connectCommand to set
	 */
	@Inject
	public void setCommandConnect(@Named("ConnectCommand") ICommand connectCommand) {
		this.commandConnect = connectCommand;
	}

	/**
	 * @return the connectCommand
	 */
	public ICommand getCommandConnect() {
		return commandConnect;
	}

	@org.jdesktop.application.Action
	public void showConectionDialog(){
		ICommand command = MainFrame.this.getCommandConnect();

		if(command != null) {
			command.Execute();
		}
	}

	/**
	 * This method initializes jMenuItemExit	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemExit() {
		if (jMenuItemExit == null) {
			jMenuItemExit = new JMenuItem();
			jMenuItemExit.setText("Exit");
			jMenuItemExit.setAction(this.getExitAction());
		}
		return jMenuItemExit;
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.IMainView#isConnectActionEnabled()
	 */
	@Override
	public boolean isConnectActionEnabled() {
		return this.getConnectAction().isEnabled();
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.IMainView#setConnectActionEnabled(boolean)
	 */
	@Override
	public void setConnectActionEnabled(boolean enabled) {
		this.getConnectAction().setEnabled(enabled);
	}

}
